package action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;

import po.Address;
import po.User;


import bo.HibernateSessionFactory;

public class AddressUpdateSeeAction {
	private int friend = 0;
	private int relation = 0;
	private int colleague = 0;
	private int netfriend = 0;
	private int common = 0;
	private int other = 0;
	private int total = 0;
	private int id;
	
	private String sign;
	private String name;
	private String email;
	private String belong;
	
	public String execute(){
		Session session2 = HibernateSessionFactory.getSession();
		Address aa = (Address)session2.get(Address.class, id);
		this.setName(aa.getName());
		this.setEmail(aa.getEmail());
		this.setBelong(aa.getBelong());
		this.setId(id);
		this.setSign(sign);
		session2.close();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		Session session = HibernateSessionFactory.getSession();
		User u = (User)session.get(User.class, (Integer)request.getSession().getAttribute("id"));
			Set set = u.getAddresses();
			Iterator it = set.iterator();
			while(it.hasNext()){
				Address a = (Address)it.next();
			if(a.getBelong().equals("朋友")){
				friend++;
			}
			if(a.getBelong().equals("亲人")){
				relation++;
			}
			if(a.getBelong().equals("同事")){
				colleague++;
			}
			if(a.getBelong().equals("网友")){
				netfriend++;
			}
			if(a.getBelong().equals("常用联系人")){
				common++;
			}
			if(a.getBelong().equals("其他")){
				other++;
			}
		}
		total =friend+relation+colleague+netfriend+common+other;
		this.setTotal(total);
		this.setColleague(colleague);
		this.setCommon(common);
		this.setFriend(friend);
		this.setNetfriend(netfriend);
		this.setRelation(relation);
		this.setOther(other);
		session.close();
		return "address";
	}

	public int getFriend() {
		return friend;
	}

	public void setFriend(int friend) {
		this.friend = friend;
	}

	public int getRelation() {
		return relation;
	}
	
	

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setRelation(int relation) {
		this.relation = relation;
	}

	public int getColleague() {
		return colleague;
	}

	public void setColleague(int colleague) {
		this.colleague = colleague;
	}

	public int getNetfriend() {
		return netfriend;
	}

	public void setNetfriend(int netfriend) {
		this.netfriend = netfriend;
	}

	public int getCommon() {
		return common;
	}

	public void setCommon(int common) {
		this.common = common;
	}

	public int getOther() {
		return other;
	}

	public void setOther(int other) {
		this.other = other;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getBelong() {
		return belong;
	}

	public void setBelong(String belong) {
		this.belong = belong;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
	
}
